import 'dart:io';

import 'package:ladder_cash/app/com/manage/data_upload_manage/data_upload_manage.dart';
import 'package:ladder_cash/app/com/plugin/common_plugin.dart';
import 'package:ladder_cash/app/com/plugin/device_data_plugin.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/com/http/Res.dart';
import 'package:ladder_cash/app/com/manage/location_manage/location_info.dart';
import 'package:ladder_cash/app/com/manage/location_manage/location_manage.dart';
import 'package:liuxlcommon/util/string_utils.dart';
import 'package:ladder_cash/app/key_mix.dart';

enum BPoint {
  register,
  face,
  personage,
  job,
  contact,
  bank,
  startApply,
  endApply,
  productAuth,
  questionnaire,
  idPan,
  idCardType,
  idFront,
  idBack,
}

class BurialPointService {
  static Map<int, Map<String, dynamic>> bpMap = {};
  ///菲律宾
  static var bpmap = {
    BPoint.register:  1,
    BPoint.productAuth: 1,
    BPoint.questionnaire: 2,
    // BPoint.idCardType: Platform.isAndroid ? 2 : 2,
    BPoint.idFront:   3,
    // BPoint.idBack: Platform.isAndroid ? 4 : 4,
    BPoint.face: 6,
    BPoint.personage: 7,
    BPoint.job: 8,
    BPoint.contact: 9,
    BPoint.bank: 10,
    BPoint.startApply: 11,
    BPoint.endApply: 12,
  };

  static Future<void> start(BPoint bPoint, {ache, waitLocation = false}) async {
    if (Platform.isAndroid) {
      if (bPoint == BPoint.productAuth) return;
    }
    await BurialPointService()
        .pointStart(bpmap[bPoint] ?? 0, ache: ache, waitLocation: waitLocation);
  }

  static Future<void> end(BPoint bPoint, {ache,orderNo}) async {
    if (Platform.isAndroid) {
      if (bPoint == BPoint.productAuth) return;
    }
    await BurialPointService().pointEnd(bpmap[bPoint] ?? 0, ache: ache,orderNo: orderNo);
  }

  ///  1、产品授权之后，2.问卷调查，3、身份证正面OCR，4、身份证反面ocr，5、手持身份证照片，6、活体认证，7、个人信息，8、工作信息，9、联系人，10、银行卡，11、开始申贷 12、结束申贷
  Future<void> pointStart(int teach, {ache, waitLocation = false}) async {
    if(teach==0){
      print("asdfa");
    }
    var startTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    Map<String, dynamic> map =
        await getParams(teach, waitLocation: waitLocation);
    map['startTime'.hx] = startTime;
    map['productId'.hx] = ache;
    bpMap[teach] = map;
  }

  ///  1、产品授权之后，2.问卷调查，3、身份证正面OCR，4、身份证反面ocr，5、手持身份证照片，6、活体认证，7、个人信息，8、工作信息，9、联系人，10、银行卡，11、开始申贷 12、结束申贷
  pointEnd(int teach, {ache,orderNo}) async {
    Map<String, dynamic>? map = bpMap[teach];
    map ??= await getParams(teach, isLocation: false);
    map['productId'.hx] = ache;
    map['orderNo'.hx] = orderNo;
    map['endTime'.hx] = DateTime.now().millisecondsSinceEpoch ~/ 1000;

    if (teach == bpmap[BPoint.startApply] || teach == bpmap[BPoint.endApply]) {
      map['startTime'.hx] = map['endTime'.hx];
    }

    if (StringUtils.isEmpty(map['longitude'.hx])) {
      LocationInfo? locationInfo =
          await LocationManage.instance.determinePosition();
      if (locationInfo != null) {
        map['longitude'.hx] = StringUtils.toFixed(locationInfo.longitude, 6);
        map['latitude'.hx] = StringUtils.toFixed(locationInfo.latitude, 6);
      }
    }
    if(StringUtils.isEmpty(map['longitude'.hx])){
      map['longitude'.hx] = "0.000000";
      map['latitude'.hx] ="0.000000";
    }
    pointRequest(teach);
  }

  Future<Map<String, dynamic>> getParams(int teach,
      {isLocation = true, waitLocation = false}) async {
    Map<String, dynamic> map = {};
    map['sceneType'.hx] = teach;
    map['client_type'.hx] = Platform.isAndroid ? 1 : 2; //1=安卓 2=ios
    map['deviceNo'.hx] = Platform.isAndroid
        ? await DeviceDataPlugin.getAndroidId
        : await DeviceDataPlugin.getIdfv; //1安卓传and_id ios传idfv
    // map['equipment_b'.hx] = Platform.isAndroid
    //     ? await DeviceDataPlugin.getGdid
    //     : await DeviceDataPlugin.getIdfa; //	安卓传gaid ios传idfa
    if (isLocation) {
      if (waitLocation) {
        LocationInfo? locationInfo =
            await LocationManage.instance.determinePosition();
        if (locationInfo != null) {
          map['longitude'.hx] = StringUtils.toFixed(locationInfo.longitude, 6);
          map['latitude'.hx] = StringUtils.toFixed(locationInfo.latitude, 6);
        }
      } else {
        LocationManage.instance.determinePosition().then((locationInfo) {
          if (locationInfo != null) {
            map['longitude'.hx] =
                StringUtils.toFixed(locationInfo.longitude, 6);
            map['latitude'.hx] = StringUtils.toFixed(locationInfo.latitude, 6);
          }
        });
      }
    }
    return map;
  }

  pointRequest(int teach) async {
    Map<String, dynamic>? map = bpMap[teach]!;
    if (map == null) return;

    String action = '/v3/buried-point/upload'.hx;
    DataUploadManage.instance.add(action, map);
    // Res r =
    //     await HttpRequest().post(action, map, parse: false, autoToast: false);
    // if (r.success) {
    // } else {
    //   // PrintUtils.p(r.msg);
    // }
  }
}
